﻿#region Librerias
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
#endregion

/// <summary>
/// Class Course, dependiente de la clase Person, define una persona natural
/// </summary>
//tratamiento especial para AGE
#region Class

public class clsCourse : DAC {
    #region Variables

    private int idCourse;
    private String sName, sDescription, sUrlDoc, dtDate;    
    private const String tabla = "dbo.tblCourse";

    #endregion

    #region Builder

    /// <summary>
    /// Constructor
    /// </summary>
    public clsCourse() { }

    #endregion

    #region Methods

    #region Consultations

    public DataSet SeeAllTheRecords() {
        String query = "SELECT row_number() over(order by " + tabla + ".idCourse) As NUMBER, ";
        query += tabla + ".idCourse As ID, " + tabla + ".sName As NAME, ";
        query += tabla + ".sDescription As DESCRIPTION, " + tabla + ".sUrlDoc As URLDOC, ";
        query += tabla + ".dtDate As DATE ";
        query += "FROM " + tabla;
        return this.ExecuteQuery(query);
    }

    public DataSet SeeAllTheRecordsDesc() {
        String query = "SELECT row_number() over(order by " + tabla + ".idCourse) As NUMBER, ";
        query += tabla + ".idCourse As ID, " + tabla + ".sName As NAME, ";
        query += tabla + ".sDescription As DESCRIPTION, " + tabla + ".sUrlDoc As URLDOC, ";
        query += tabla + ".dtDate As DATE ";
        query += "FROM " + tabla + " ";
        query += "ORDER BY " + tabla + ".idCourse DESC";
        return this.ExecuteQuery(query);
    }

    public DataSet SeeAllTheRecords(int startRecord, int maxRecord, String text){
        String query = "SELECT row_number() over(order by " + tabla + ".idCourse) As NUMBER, ";
        query += tabla + ".idCourse As ID, " + tabla + ".sName As NAME, ";
        query += tabla + ".sDescription As DESCRIPTION, " + tabla + ".sUrlDoc As URLDOC, ";
        query += tabla + ".dtDate As DATE ";
        query += "FROM " + tabla;
        if(!text.Equals("")){
            query += " WHERE " + tabla + ".sName LIKE '%" + text + "%' ";
            query += "OR " + tabla + ".sDescription LIKE '%" + text + "%'";
        }
        if (startRecord == 0 && maxRecord == 0) {
            return this.ExecuteQuery(query);
        }
        else {
            return this.ExecuteQuery(query, startRecord, maxRecord);
        }        
    }

    public DataSet ConsultID(String id) {
        String query = "SELECT ";
        query += tabla + ".idCourse As ID, " + tabla + ".sName As NAME, ";
        query += tabla + ".sDescription As DESCRIPTION, " + tabla + ".sUrlDoc As URLDOC, ";
        query += tabla + ".dtDate As DATE ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idCourse =" + id;
        return this.ExecuteQuery(query);
    }

    #endregion

    public int Insert() {
        String query = "INSERT INTO " + tabla;
        query += "(sName, sDescription, sUrlDoc, dtDate) ";
        query += "VALUES ('{0}','{1}','{2}','{3}') SELECT CAST(scope_identity() AS int)";

        String sql = String.Format(query, this.SName, this.SDescription, this.SUrlDoc, this.DtDate);
        return this.ExecuteDML(sql, "no");
    }

    public int Update(){
        String query = "UPDATE " + tabla + " SET ";        
        query += tabla + ".sName='" + this.SName + "', " + tabla + ".sDescription='" + this.SDescription + "', ";
        query += tabla + ".sUrlDoc='" + this.SUrlDoc + "', " + tabla + ".dtDate='" + this.DtDate + "' ";
        query += "WHERE " + tabla + ".idCourse=" + this.ID;
        return this.ExecuteDML(query, "no");
    }

    public int Delete()
    {
        String query = "DELETE FROM " + tabla + " WHERE " + tabla + ".idCourse={0}";
        String sql = String.Format(query, this.ID);
        return this.ExecuteDML(sql, "no");
    }

    #endregion

    #region Setters and Getters

    
    public int ID
    {
        get { return idCourse; }
        set { idCourse = value; }
    }

    
    public String SName
    {
        get { return sName; }
        set { sName = value; }
    }
    
    public String DtDate {
        get { return dtDate; }
        set { dtDate = value; }
    }

    public String SUrlDoc {
        get { return sUrlDoc; }
        set { sUrlDoc = value; }
    }

    public String SDescription {
        get { return sDescription; }
        set { sDescription = value; }
    }
    #endregion

}

#endregion